Imports System.Configuration
Imports System.Web.Configuration

Public Class ForumService
    Private Shared _provider As ForumProvider = Nothing
    Private Shared _providers As ForumCollection = Nothing
    Private Shared _lock As Object = New Object
    Private Shared isInitialized As Boolean = False
    Private Shared initializationException As Exception

    Public ReadOnly Property Provider() As ForumProvider
        Get
            Return _provider
        End Get
    End Property

    Public ReadOnly Property Providers() As ForumCollection
        Get
            Return _providers
        End Get
    End Property

    Private Shared Sub LoadProviders()
        Try
            'Get the feature's configuration info
            Dim qc As ForumConfiguration = CType(ConfigurationManager.GetSection("ForumProvider"), ForumConfiguration)
            If ((qc.DefaultProvider Is Nothing) _
                        OrElse ((qc.Providers Is Nothing) _
                        OrElse (qc.Providers.Count < 1))) Then
                Throw New System.Configuration.Provider.ProviderException(("You must specify a valid default provider."))
            End If
            _providers = New ForumCollection
            ProvidersHelper.InstantiateProviders(qc.Providers, _providers, GetType(ForumProvider))
            _providers.SetReadOnly()
            _provider = _providers(qc.DefaultProvider)
            If (_provider Is Nothing) Then
                Throw New ConfigurationErrorsException("You must specify a default provider for the feature.", qc.ElementInformation.Properties("defaultProvider").Source, qc.ElementInformation.Properties("defaultProvider").LineNumber)
            End If
        Catch ex As Exception
            initializationException = ex
            isInitialized = True
            Throw ex
        End Try
        isInitialized = True
        'error-free initialization
    End Sub

    Public Shared Function GetForums() As List(Of DataTypes.ForumDetails)
        LoadProviders()
        Return _provider.GetForums
    End Function
    Public Shared Function GetForumByID(ByVal forumID As String) As DataTypes.ForumDetails
        LoadProviders()
        Return _provider.GetForumByID(forumID)
    End Function
    Public Shared Function DeleteForum(ByVal forumID As String) As Boolean
        LoadProviders()
        Return _provider.DeleteForum(forumID)
    End Function
    Public Shared Function UpdateForum(ByVal forum As DataTypes.ForumDetails) As Boolean
        LoadProviders()
        Return _provider.UpdateForum(forum)
    End Function
    Public Shared Function InsertForum(ByVal title As String, ByVal moderated As Boolean, _
                ByVal importance As Integer, ByVal description As String, ByVal imageUrl As String, ByVal Username As String) As String
        LoadProviders()
        Return _provider.InsertForum(title, moderated, importance, description, imageUrl, Username)
    End Function

    ' methods that work with posts
    Public Shared Function GetThreads(ByVal forumID As String, ByVal sortExpression As String, _
        ByVal pageIndex As Integer, ByVal pageSize As Integer) As List(Of DataTypes.PostDetails)
        LoadProviders()
        Return _provider.GetThreads(forumID, sortExpression, pageIndex, pageSize)
    End Function
    Public Shared Function GetThreadCount(ByVal forumID As String) As Integer
        LoadProviders()
        Return _provider.GetThreadCount(forumID)
    End Function
    Public Shared Function GetThreads(ByVal ForumID As String) As List(Of DataTypes.PostDetails)
        LoadProviders()
        Return _provider.GetThreads(ForumID)
    End Function
    Public Shared Function GetThreads(ByVal sortExpression As String, _
        ByVal pageIndex As Integer, ByVal pageSize As Integer) As List(Of DataTypes.PostDetails)
        LoadProviders()
        Return _provider.GetThreads(sortExpression, pageIndex, pageSize)
    End Function
    Public Shared Function GetThreadCount() As Integer
        LoadProviders()
        Return _provider.GetThreadCount
    End Function
    Public Shared Function GetThreadByID(ByVal threadPostID As String) As List(Of DataTypes.PostDetails)
        LoadProviders()
        Return _provider.GetThreadByID(threadPostID)
    End Function
    Public Shared Function GetPostCountByThread(ByVal threadPostID As String) As Integer
        LoadProviders()
        Return _provider.GetPostCountByThread(threadPostID)
    End Function
    Public Shared Function GetPostCountByForum(ByVal forumID As String) As Integer
        LoadProviders()
        Return _provider.GetPostCountByForum(forumID)
    End Function
    Public Shared Function GetUnapprovedPosts() As List(Of DataTypes.PostDetails)
        LoadProviders()
        Return _provider.GetUnapprovedPosts
    End Function
    Public Shared Function GetPostByID(ByVal postID As String) As DataTypes.PostDetails
        LoadProviders()
        Return _provider.GetPostByID(postID)
    End Function
    Public Shared Function DeletePost(ByVal postID As String) As Boolean
        LoadProviders()
        Return _provider.DeletePost(postID)
    End Function
    Public Shared Function UpdatePost(ByVal post As DataTypes.PostDetails) As Boolean
        LoadProviders()
        Return _provider.UpdatePost(post)
    End Function
    Public Shared Function InsertPost(ByVal post As DataTypes.PostDetails) As String
        LoadProviders()
        Return _provider.InsertPost(post)
    End Function
    Public Shared Function ApprovePost(ByVal postID As String) As Boolean
        LoadProviders()
        Return _provider.ApprovePost(postID)
    End Function
    Public Shared Function CloseThread(ByVal threadPostID As String) As Boolean
        LoadProviders()
        Return _provider.CloseThread(threadPostID)
    End Function
    Public Shared Function MoveThread(ByVal threadPostID As String, ByVal forumID As String) As Boolean
        LoadProviders()
        Return _provider.MoveThread(threadPostID, forumID)
    End Function
    Public Shared Function IncrementPostViewCount(ByVal postID As String) As Boolean
        LoadProviders()
        Return _provider.IncrementPostViewCount(postID)
    End Function
    Public Shared Function GetPostBody(ByVal postID As String) As String
        LoadProviders()
        Return _provider.GetPostBody(postID)
    End Function

    Public Shared Function GetLatestPost(ByVal forumID As String) As DataTypes.PostDetails
        LoadProviders()
        Return _provider.GetLatestPost(forumID)
    End Function

End Class
